define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var fetchProperties = require("../constant/fetchProperties");
	var processData = require("../store/processData");
	
    var instance = {
        initComponent: function (params,widget) {
        	var component = new Grid({
                parentNode: ".J-grid", 
                autoRender:false,
                model : {
                	id:"grid",
                    url : "api/checkin/queryadjust",                    
                    params: function() {
                    	var subnav=widget.get("subnav");
                    	var selectDestination = subnav.getValue("destination");
                    	var destinations = subnav.getData("destination");
                    	var selectDestData = null;
                    	for(var i = 0; i < destinations.length; i++){
                    		if(destinations[i].strKey == selectDestination){
                    			selectDestData = {
                					pkDestnAndSerp : destinations[i].pkDestnAndSerp,
                					isSerPoint : destinations[i].isSerPoint
                    			}
                    		}
                    	}
                        return {
                        	"destnSerPoint":selectDestData.pkDestnAndSerp + "," + selectDestData.isSerPoint,
                        	"groupReserve.checkInDate":subnav.getValue("dateRange").start,
     						"groupReserve.checkOutDate":subnav.getValue("dateRange").end,
     						fetchProperties: fetchProperties.queryadjust
                        };
                    },
                    columns : [{
                    	name:"destinationName",
        				label:"目的地",
        				format : function(value, row){
        					if(row.planItemGroup){
        						return row.planItemGroup.planItem.destination.name;
        					}else{
        						return row.nonMemberGroup.servicePoint.name;
        					}
        				}
        			},{
						name:"stareDate",
						label:"开始日期",
						format:function(value, row){
        					if(row.planItemGroup){
        						var stareDate = row.planItemGroup.planItem.startDate;
        						return moment(stareDate).format("YYYY-MM-DD");
        					}else{
        						var checkInDate = row.nonMemberGroup.checkInDate;
        						return moment(checkInDate).format("YYYY-MM-DD");
        					}
						}
					},{
						name:"endDate",
						label:"结束日期",
						format:function(value, row){
        					if(row.planItemGroup){
        						var endDate = row.planItemGroup.planItem.endDate;
        						return moment(endDate).format("YYYY-MM-DD");
        					}else{
        						var checkOutDate = row.nonMemberGroup.checkOutDate;
        						return moment(checkOutDate).format("YYYY-MM-DD");
        					}
						}
					},{
        				name:"signUpNumber",
        				label:"组团类型",
        				format:function(value,row){
        					if(row.planItemGroup){
        						return row.groupType.value;
        					}else{
        						if(row.nonMemberGroup.signUpType.key == "NonMember"){
	        						return row.nonMemberGroup.tourType.value;
	        					}else{
	        						return "体验客户";
	        					}
        					}
        				}
					},{						
						name:"groupReserve",
        				label:"组团单位/预约人",
        				format: function(value,row){
        					if(row.planItemGroup){
        						if(row.planItemGroup.allianceOrg){
        							return row.planItemGroup.allianceOrg.name;
        						}else{
        							return row.planItemGroup.servicePoint.name
        						}
        					}else{
    							return row.nonMemberGroup.contactName;
        					}
        				}
					},{
        				name:"groupStatus.value",
        				label:"当前阶段",
        			},{
						name:"adjustTypes",
						label:"调整类型",
						format:function(value, row){
							if(row.planItemGroup){
								var adjustTypes = row.planItemGroup.adjustTypes;
								return processData.adjustType(adjustTypes);
        					}else{
        						var adjustTypes = row.nonMemberGroup.adjustTypes;
        						return processData.adjustType(adjustTypes);
        					}
        				}
					},{
						name:"adjustDate",
						label:"调整日期",
						format:function(value, row){
							var adjustDate = "";
        					if(row.planItemGroup){
        						adjustDate = row.planItemGroup.adjustDate;
        					}else{
        						adjustDate = row.nonMemberGroup.adjustDate;
        					}
        					
							if(adjustDate){
    							return moment(adjustDate).format("YYYY-MM-DD");
    						}else{
    							return "";
    						}
						}
					},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"adjustment",
							text: "调整",
							show:function(value,row){		
								if(row.planItemGroup){
									var adjustTypes = row.planItemGroup.adjustTypes;
									return processData.adjustmentBtn(adjustTypes);	        						
	        					}else{
	        						var adjustTypes = row.nonMemberGroup.adjustTypes;
	        						return processData.adjustmentBtn(adjustTypes);
	        					}
							},
							handler:function(index,data,rowEle){
								widget.set("check",false);
								widget.handler.adjustmentEvent(widget,data);
							}
						},{
							id:"check",
							text: "查看",
							show:function(value,row){
								return true;
							},
							handler:function(index,data,rowEle){
								//渲染子列表
								widget.set("check",true);
								widget.handler.adjustmentEvent(widget,data);
								//隐藏操作按钮
								var form = widget.get("adjsutForm"),
									subnav = widget.get("subnav"),
									subGrid = widget.get("subGrid");
								subnav.hide(["save","cancel"]);
								$("#applyAdd").hide();
								form.setDisabled(true);
								$(".J-grid-subGrid-exit").hide();
							}
						}]
					}]
                }
            });
            widget.set("grid",component);
        }
    };
    module.exports = instance;
});